package soundwave.com.demo.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import soundwave.com.demo.R;
import soundwave.com.demo.adpater.Viewpager_adapert;
import soundwave.com.demo.entry.LoginSingle;
import soundwave.com.demo.list.HeadlinesList;
import soundwave.com.demo.list.LoginList;
import soundwave.com.demo.list.TakeoutfoodGrid;
import soundwave.com.demo.list.TakeoutfoodGrid_item;
import soundwave.com.demo.list.ViewpagerList;
import soundwave.com.demo.test.Imageview_test;
import soundwave.com.demo.test.Viewpager_test;


public class MainActivity extends Activity {
    private Context context = this;
    private TextView city;
    private ImageView img;
    private TextView my;
    private ViewPager viewpager;
    private LinearLayout viewgroup;
    private ImageView headimg;
    private RelativeLayout layout;
    private ImageView imageview1;
    private TextView headline1;
    private ImageView imageview2;
    private TextView headline2;
    private TextView meishiwaimai;
    private TextView more;
    private ImageView flower;
    private ImageView run;
    private ImageView specialty;
    private ImageView business;
    private LinearLayout activity_main;
    private ArrayList<Imageview_test> imges;
    private ImageView[] tips;
    private ImageView[] imageViews;
    private LinearLayout headlines;
    private RelativeLayout myimg;
    private RelativeLayout takeoutfoodimg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        onclik();
        shared();
        setviewpager();


    }

    private void initView() {
        city = (TextView) findViewById(R.id.city);
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        viewgroup = (LinearLayout) findViewById(R.id.viewgroup);
        headimg = (ImageView) findViewById(R.id.headimg);
        layout = (RelativeLayout) findViewById(R.id.layout);
        imageview1 = (ImageView) findViewById(R.id.imageview1);
        headline1 = (TextView) findViewById(R.id.headline1);
        imageview2 = (ImageView) findViewById(R.id.imageview2);
        headline2 = (TextView) findViewById(R.id.headline2);
        meishiwaimai = (TextView) findViewById(R.id.meishiwaimai);
        more = (TextView) findViewById(R.id.more);
        flower = (ImageView) findViewById(R.id.flower);
        specialty = (ImageView) findViewById(R.id.specialty);
        activity_main = (LinearLayout) findViewById(R.id.activity_main);
        headlines = (LinearLayout) findViewById(R.id.headlines);

        myimg = (RelativeLayout) findViewById(R.id.myimg);

        takeoutfoodimg = (RelativeLayout) findViewById(R.id.takeoutfoodimg);

    }

    private void onclik() {
        myimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, LoginList.class));
            }
        });


        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                setImageBackground(position % imageViews.length);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        headlines.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, HeadlinesList.class));
            }
        });
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(context, TakeoutfoodGrid.class));
            }
        });
        takeoutfoodimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context,TakeoutfoodGrid.class));
            }
        });
    }


    private void shared() {
        SharedPreferences sharedPreferences = getSharedPreferences("login", MODE_APPEND);
        boolean value = sharedPreferences.getBoolean("isLogin", false);
        String name = sharedPreferences.getString("isName", "未登陆");
        String phone = sharedPreferences.getString("Phone", "");
        LoginSingle loginSingle = LoginSingle.getInstance();
        loginSingle.setLogin(value);
        loginSingle.setName(name);
        loginSingle.setPhone(phone);


    }

    private void setviewpager() {
        imges = new ArrayList<>();
        Imageview_test img1 = new Imageview_test();
        img1.setViewpager(R.mipmap.coupon);
        imges.add(img1);
        Imageview_test img2 = new Imageview_test();
        img2.setViewpager(R.mipmap.code);
        imges.add(img2);
        Imageview_test img3 = new Imageview_test();
        img3.setViewpager(R.mipmap.ic_launcher);
        imges.add(img3);


        tips = new ImageView[imges.size()];
        for (int i = 0; i < tips.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(5, 5));

            tips[i] = imageView;
            if (i == 0) {
                tips[i].setBackgroundResource(R.mipmap.spot_black);
            } else {
                tips[i].setBackgroundResource(R.mipmap.spot_white);
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin = 5;
            layoutParams.rightMargin = 5;
            viewgroup.addView(imageView, layoutParams);

        }

        imageViews = new ImageView[imges.size()];
        for (int i = 0; i < imageViews.length; i++) {
            final int num = i;
            ImageView imageView = new ImageView(this);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Viewpager_test viewpager_test = new Viewpager_test();
                    viewpager_test.setTitle(String.valueOf(num));

                    Intent intent = new Intent(context, ViewpagerList.class);
                    intent.putExtra("viewpager_test", viewpager_test);
                    startActivity(intent);
                }
            });
            imageViews[i] = imageView;
            imageView.setBackgroundResource(imges.get(i).getViewpager());
        }

        Viewpager_adapert viewpager_adapert = new Viewpager_adapert(imges, imageViews);
        viewpager.setAdapter(viewpager_adapert);
        viewpager.setCurrentItem(0);


    }

    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setBackgroundResource(R.mipmap.spot_black);
            } else {
                tips[i].setBackgroundResource(R.mipmap.spot_white);
            }
        }

    }
}
